package com.fh.shop.api.util;

import redis.clients.jedis.Jedis;

import java.util.Map;

public class RedisUtil {
    //redis赋值方法
    public static void set(String key,String value){
        Jedis connection = null;
        try {
            //获取redis连接
            connection = RedisPool.getConnection();
            connection.set(key,value);
        } catch (Exception e) {
            e.printStackTrace();
//            throw new RuntimeException(e);
        } finally {
            //关流
            if (connection != null){
                connection.close();
            }
        }
    }

    //redis存放hash
    public static void hset(String key,String field,String value){
        Jedis connection = null;
        try {
            //获取redis连接
            connection = RedisPool.getConnection();
            connection.hset(key,field,value);
        } catch (Exception e) {
            e.printStackTrace();
//            throw new RuntimeException(e);
        } finally {
            //关流
            if (connection != null){
                connection.close();
            }
        }
    }

    //redis获取hash
    public static String hget(String key,String field){
        Jedis connection = null;
        String value = "";
        try {
            //获取redis连接
            connection = RedisPool.getConnection();
            value = connection.hget(key,field);
        } catch (Exception e) {
            e.printStackTrace();
//            throw new RuntimeException(e);
        } finally {
            //关流
            if (connection != null){
                connection.close();
            }
        }
        return value;
    }

    //redis批量存值
    public static void hmset(String key, Map<String,String> map){
        Jedis connection = null;
        try {
            //获取redis连接
            connection = RedisPool.getConnection();
            connection.hmset(key,map);
        } catch (Exception e) {
            e.printStackTrace();
//            throw new RuntimeException(e);
        } finally {
            //关流
            if (connection != null){
                connection.close();
            }
        }
    }

    //设置过期时间
    public static void expire(String key,int seconds){
        Jedis connection = null;

        try {
            //获取redis连接
            connection = RedisPool.getConnection();
            connection.expire(key,seconds);
        } catch (Exception e) {
            e.printStackTrace();
//            throw new RuntimeException(e);
        } finally {
            //关流
            if (connection != null){
                connection.close();
            }
        }
    }

    //查看是否过期
    public static boolean exists(String key){
        Jedis connection = null;
        Boolean exists = false;
        try {
            //获取redis连接
            connection = RedisPool.getConnection();
            exists = connection.exists(key);
            return exists;
        } catch (Exception e) {
            e.printStackTrace();
//            throw new RuntimeException(e);
        } finally {
            //关流
            if (connection != null){
                connection.close();
            }
        }
        return exists;
    }

    //redis赋值并设置过期时间
    public static void setex(String key,String value,int seconds){
        Jedis connection = null;
        try {
            //获取redis连接
            connection = RedisPool.getConnection();
            connection.setex(key,seconds,value);
        } catch (Exception e) {
            e.printStackTrace();
//            throw new RuntimeException(e);
        } finally {
            //关流
            if (connection != null){
                connection.close();
            }
        }
    }

    //清除redisi
    public static void del(String key){
        Jedis connection = null;
        try {
            //获取redis连接
            connection = RedisPool.getConnection();
            connection.del(key);
        } catch (Exception e) {
            e.printStackTrace();
//            throw new RuntimeException(e);
        } finally {
            //关流
            if (connection != null){
                connection.close();
            }
        }
    }

    //redis取值方法
    public static String get(String key){
        Jedis connection = null;
        String redis = "";
        try {
            //获取redis连接
            connection = RedisPool.getConnection();
            return connection.get(key);
        } catch (Exception e) {
            e.printStackTrace();
//            throw new RuntimeException(e);
        } finally {
            //关流
            if (connection != null){
                connection.close();
            }
        }
        return redis;
    }


}
